package org.itboys.product.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.itboys.product.dto.CommonTouristInfoDTO;
import org.itboys.product.entity.CommonTouristInfo;
import org.itboys.product.entity.ReceiveAddress;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by DELL on 2016/7/5.
 * 常用旅客业务
 */
@Service
public class CommonTouristInfoService extends BaseService<CommonTouristInfo, Long> {
    @Resource(name = "productDS")
    private MongoDataSource touristDS;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return touristDS;
    }

    @Override
    protected Class<CommonTouristInfo> getEntityClass() {
        return CommonTouristInfo.class;
    }

    /**
     * 新增旅客信息/编辑旅客信息
     *
     * @param touristId
     * @param memberId
     * @param name
     * @param mobile
     * @param idNumber
     */
    public void save(Long touristId, Long memberId, String name, String mobile,Integer idType, String idNumber,Integer type) {
        CommonTouristInfo commonTouristInfo;
        if (touristId == null) {
            commonTouristInfo = new CommonTouristInfo();
            commonTouristInfo.setMemberId(memberId);
            commonTouristInfo.setName(name);
            commonTouristInfo.setMobile(mobile);
            commonTouristInfo.setIdNumber(idNumber);
            commonTouristInfo.setIdType(idType);
            commonTouristInfo.setType(type);
            commonTouristInfo.setCreateTime(new Date());
            commonTouristInfo.setUpdateTime(new Date());
            //如果之前没有信息的话,要把这个地址设为默认地址
            List<CommonTouristInfo> info = getInfoByMemberId(memberId);
            if (info == null || info.isEmpty()) {
                commonTouristInfo.setIsDeleted(2);
            }
            super.save(commonTouristInfo);

        } else {
            commonTouristInfo = getById(touristId);
            if (StringUtils.isNotBlank(name)) {
                commonTouristInfo.setName(name);
            }
            if (StringUtils.isNotBlank(mobile)) {
                commonTouristInfo.setMobile(mobile);
            }
            if (StringUtils.isNotBlank(idNumber)) {
                commonTouristInfo.setIdNumber(idNumber);
            }
            if (null!=idType) {
                commonTouristInfo.setIdType(idType);
            }
            if (null!=type) {
                commonTouristInfo.setType(type);
            }
            super.update(commonTouristInfo);
        }
    }

    //获取旅客的信息
    public List<CommonTouristInfo> getInfoByMemberId(Long memberId) {
        Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
        param.put("memberId", memberId);
        param.put("isDeleted !=", 1);
        return super.list(param);
    }

    //获取默认信息
    public CommonTouristInfo getTouristDefaultInfo(Long memberId) {
        Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
        param.put("memberId", memberId);
        param.put("isDeleted", 2);
        List<CommonTouristInfo> list = super.list(param);
        return list != null && !list.isEmpty() ? list.get(0) : null;
    }

    //设置默认旅客信息
    public void setDefaultInfo(Long memberId, Long touristId){
        //先找到原来默认旅客信息
        CommonTouristInfo defaultInfo = getTouristDefaultInfo(memberId);
        CommonTouristInfo info = getById(touristId);
        info.setIsDeleted(2);
        if (defaultInfo!=null){//如果有默认旅客信息，就把原来的默认旅客信息取消掉
            defaultInfo.setIsDeleted(0);
            List<CommonTouristInfo> list = Lists.newArrayListWithExpectedSize(2);
            list.add(defaultInfo);
            list.add(info);
            super.batchUpdate(list);
        }else {
            super.update(info);
        }
    }

    //删除地址
    public void deleteInfo(Long touristId){
        CommonTouristInfo info = getById(touristId);
        if (info.getIsDeleted().equals(2)){
            Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
            param.put("memberId", info.getMemberId());
            param.put("isDeleted",2);
            List<CommonTouristInfo> list = super.list(param);
            if (list == null || list.isEmpty()){
                super.update(touristId, "isDeleted", 1);
            } else {
                CommonTouristInfo defaultInfo = list.get(0);
                defaultInfo.setIsDeleted(2);
                info.setIsDeleted(1);
                list = Lists.newArrayListWithExpectedSize(2);
                list.add(defaultInfo);
                list.add(info);
                super.batchUpdate(list);
            }
        } else {
            super.update(touristId, "isDeleted", 1);
        }
    }

    public CommonTouristInfoDTO findById(Long id){
        CommonTouristInfoDTO dto = new CommonTouristInfoDTO();
        CommonTouristInfo info = getById(id);
        filled(dto, info);
        return dto;
    }

    //将info中信息填充到dto中
    public void filled(CommonTouristInfoDTO dto,CommonTouristInfo info){
        dto.setId(info.getId());
        dto.setName(info.getName());
        dto.setMobile(info.getMobile());
        dto.setIdNumber(info.getIdNumber());
        dto.setMemberId(info.getMemberId());
        dto.setIsDeleted(info.getIsDeleted());
    }

}
